"""
BaoStock日K数据实体类

本文件定义BaoStock日K数据的数据模型，映射到数据库表stock_baostock_daily。
相关知识点：
- SQLAlchemy异步ORM
- 数据库实体映射
"""
from sqlalchemy import Index, String, DateTime, Date, Integer, DECIMAL, Column, UniqueConstraint, func
from config.database import Base


class StockBaoStockDaily(Base):
    """
    BaoStock股票日线表实体类
    
    映射到数据库中的stock_baostock_daily表，存储从BaoStock获取的股票日K数据。
    """
    __tablename__ = 'stock_baostock_daily'
    
    __table_args__ = (
        UniqueConstraint("symbol", "trade_date", name="stock_baostock_daily_symbol_tradedate"),
        Index('stock_baostock_daily_symbol', 'symbol'),
        Index('stock_baostock_daily_trade_date', 'trade_date'),
    )
    
    id = Column(Integer, primary_key=True, autoincrement=True, nullable=False, comment='id')
    trade_date = Column(Date, nullable=False, comment='交易日期')
    symbol = Column(String(20), nullable=False, comment='股票代码（原始格式，如600000）')
    bs_code = Column(String(20), nullable=True, comment='BaoStock代码（如sh.600000）')
    open = Column(DECIMAL(10, 2), nullable=True, comment='开盘价')
    high = Column(DECIMAL(10, 2), nullable=True, comment='最高价')
    low = Column(DECIMAL(10, 2), nullable=True, comment='最低价')
    close = Column(DECIMAL(10, 2), nullable=True, comment='收盘价')
    preclose = Column(DECIMAL(10, 2), nullable=True, comment='前收盘价')
    volume = Column(DECIMAL(20, 0), nullable=True, comment='成交量')
    amount = Column(DECIMAL(25, 2), nullable=True, comment='成交额')
    adjustflag = Column(String(10), nullable=True, comment='复权状态（1=后复权，2=前复权，3=不复权）')
    turn = Column(DECIMAL(10, 2), nullable=True, comment='换手率')
    tradestatus = Column(String(10), nullable=True, comment='交易状态（1=正常，0=停牌）')
    pct_chg = Column(DECIMAL(10, 2), nullable=True, comment='涨跌幅')
    is_st = Column(String(10), nullable=True, comment='是否ST股（1=是，0=否）')
    create_time = Column(DateTime, nullable=False, default=func.now(), server_default=func.now(), comment='创建时间')
    update_time = Column(DateTime, nullable=False, default=func.now(), server_default=func.now(), onupdate=func.now(), comment='更新时间')

